package com.competition.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.competition.commonutils.JwtUtils;
import com.competition.commonutils.MD5;
import com.competition.login.entity.Manage;
import com.competition.login.entity.User;
import com.competition.login.mapper.ManageMapper;
import com.competition.login.service.ManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.competition.servicebase.exceptionhandler.MyException;
//import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理员登录表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-03
 */
@Service
public class ManageServiceImpl extends ServiceImpl<ManageMapper, Manage> implements ManageService {

    @Override
    public String login(Manage manager) {
        //获取登录手机号和密码
        String password = manager.getManagePassword();
        String email= manager.getManageEmail();
        //手机号和密码非空判断
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            //throw new MyiException(20001,"登录失败");
            throw new MyException(20001,"登录失败");
        }
        //判断手机号是否正确
        QueryWrapper<Manage> wrapper = new QueryWrapper<>();
        wrapper.eq("manage_email",email);
        Manage manage = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(manage == null) {//没有这个手机号
            throw new MyException(20001,"登录失败");
        }
        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(manage.getManagePassword())) {
            throw new MyException(20001,"登录失败");
        }
        //判断用户是否禁用
        if(manage.getIsBlacklist()==1) {
            throw new MyException(20001,"登录失败");
        }
        //登录成功
        //生成token字符串，使用jwt工具类（将用户id数据搞到token字符串中）
        String jwtToken = JwtUtils.getJwtToken(manage.getId(), manage.getManageUsername());
        return jwtToken;
    }
}
